package com.jcrj.service;

import com.jcrj.dao.FielTransactDao;
import com.jcrj.entity.OFlowForm;
import com.jcrj.entity.OProcessinstance;
import com.jcrj.entity.OUser;
import com.jcrj.entity.OWorkflow;
import com.jcrj.util.FielAll;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@SuppressWarnings("unchecked")
public class FielTransactService {
	private FielTransactDao fielTransactDao;

	public Long countAllFiel(String process, String name, OUser user, String startDate, String endDate) {
		StringBuffer sql = new StringBuffer();
		String hql = "from OFlowForm";
		List<OFlowForm> oflowform = this.fielTransactDao.findByHql(hql);
		sql.append("select count(t.id) from (select id,title,TO_CHAR(flow_id) as flow,to_date(to_char(create_time,'yyyy-mm-dd'),'yyyy-mm-dd') as time  from o_Document where process_id in("
				+ process + ") ");
		if (user != null)
			sql.append(" and reject='true' or DRAFT_USER=" + user.getId() + " and reject='true' and process_id is null");
		for (OFlowForm ff : oflowform) {
			if (user == null)
				sql.append(" union all select id,null,TO_CHAR(flowid),to_date(createtime,'yyyy-mm-dd') as time from " + ff.getTableName()
						+ " where instanceId in(" + process + ") ");
			else
				sql.append(" union all select id,null,TO_CHAR(flowid),to_date(createtime,'yyyy-mm-dd') as time from " + ff.getTableName()
						+ " where  reject='true' and instanceId is null and userId =" + user.getId() + " or reject='true' and instanceId in(" + process + ")");
		}
		sql.append(" ) t where 1=1 ");
		if ((name != null) && (!"".equals(name))) {
			sql.append(" and t.flow like '%" + name.trim() + "%'");
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			sql.append(" and t.time >= to_date('" + startDate + "','yyyy-mm-dd')");
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			sql.append(" and t.time <= to_date('" + endDate + "','yyyy-mm-dd')");
		}
		return Long.valueOf(this.fielTransactDao.countBySql(sql.toString()).longValue());
	}

	public Long countWorkFlowTaskFile(String process, String name, OUser user, String startDate, String endDate) {
		Long count = Long.valueOf(0L);
		StringBuffer sql = new StringBuffer();
		String hql = "from OFlowForm";
		List oflowform = this.fielTransactDao.findByHql(hql);
		sql.append("select count(t.id) from (");
		for (int i = 0; i < oflowform.size(); i++) {
			OFlowForm ff = (OFlowForm) oflowform.get(i);
			if (user == null)
				sql.append("select id,null as title,flowid,to_date(createtime,'yyyy-mm-dd') as time from " + ff.getTableName() + " where instanceId in("
						+ process + ") ");
			else {
				sql.append("select id,null as title,flowid,to_date(createtime,'yyyy-mm-dd') as time from " + ff.getTableName()
						+ " where  reject='true' and instanceId is null and userId =" + user.getId() + " or reject='true' and instanceId in(" + process + ")");
			}
			if (i != oflowform.size() - 1) {
				sql.append(" union all ");
			}
		}
		sql.append(" ) t where 1=1");
		if ((name != null) && (!"".equals(name))) {
			sql.append(" and  t.flowid like '%" + name.trim() + "%'");
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			sql.append(" and t.time >= to_date('" + startDate + "','yyyy-mm-dd')");
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			sql.append(" and t.time <= to_date('" + endDate + "','yyyy-mm-dd')");
		}
		count = Long.valueOf(this.fielTransactDao.countBySql(sql.toString()).longValue());
		return count;
	}

	public List<FielAll> getAllfiel(String process, int currentPage, String name, OUser user, String startDate, String endDate) {
		List<FielAll> faList = new ArrayList<FielAll>();
		StringBuffer sql = new StringBuffer();
		String hql = "from OFlowForm";
		List<OFlowForm> oflowform = this.fielTransactDao.findByHql(hql);
		sql.append("select * from (select id,draft_org as unit,to_date(to_char(create_time,'yyyy-mm-dd'),'yyyy-mm-dd') as time,title,draft_name as name,sfstatus as fielTyoe,content_id as sort,TO_CHAR(flow_id) as flow,PROCESS_ID,over_name from o_Document where process_id in("
				+ process + ")");
		if (user != null)
			sql.append(" and reject='true' or DRAFT_USER=" + user.getId() + " and reject='true' and process_id is null");
		for (OFlowForm ff : oflowform) {
			if (user == null)
				sql.append(" union all select id,null,to_date(createtime,'yyyy-mm-dd') as time,null,TO_CHAR(USERID),flowid,null,TO_CHAR(flowid),INSTANCEID,reject_s from "
						+ ff.getTableName() + " where instanceId in(" + process + ") ");
			else {
				sql.append(" union all select id,null,to_date(createtime,'yyyy-mm-dd') as time,null,TO_CHAR(USERID),flowid,null,TO_CHAR(flowid),INSTANCEID,reject_s from "
						+ ff.getTableName()
						+ " where  reject='true' and instanceId is null and userId ="
						+ user.getId()
						+ " or reject='true' and instanceId in(" + process + ")");
			}
		}
		sql.append(" ) t where 1=1 ");
		if ((name != null) && (!"".equals(name))) {
			sql.append(" and t.flow like '%" + name.trim() + "%'");
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			sql.append(" and t.time >= to_date('" + startDate + " 00:00:00','yyyy-mm-dd hh24:mi:ss')");
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			sql.append(" and t.time <= to_date('" + endDate + " 23:59:59','yyyy-mm-dd hh24:mi:ss')");
		}
		sql.append(" order by t.id desc");
		List<Object[]> listob = this.fielTransactDao.findByPageSql(sql.toString(), currentPage);
		for (Object[] obj : listob) {
			FielAll fa = new FielAll();
			fa.setId(Integer.valueOf(((BigDecimal) obj[0]).intValue()));
			fa.setUnit((String) obj[1]);
			fa.setTime((Date) obj[2]);
			fa.setTitle((String) obj[3]);
			fa.setSender((String) obj[4]);
			fa.setFielTyoe((String) obj[5]);
			fa.setSort((String) obj[6]);
			fa.setFlow((String) obj[7]);
			fa.setEndTime(obj[8] == null ? "1" : ((BigDecimal) obj[8]).toString());
			fa.setReject((String) obj[9]);
			faList.add(fa);
		}
		return faList;
	}

	public List getWorkFlowTaskfile(String process, int currentPage, String name, OUser user, String startDate, String endDate) {
		List faList = new ArrayList();
		StringBuffer sql = new StringBuffer();
		String hql = "from OFlowForm";
		List oflowform = this.fielTransactDao.findByHql(hql);
		sql.append("select id,unit,time,title,userid,flowid,null,flow,INSTANCEID,reject_s  from (");
		for (int i = 0; i < oflowform.size(); i++) {
			OFlowForm ff = (OFlowForm) oflowform.get(i);
			if (user == null)
				sql.append(" select id,null as unit,to_date(createtime,'yyyy-mm-dd') as time,null as title,TO_CHAR(USERID) as userid,flowid,null,flowid as flow,INSTANCEID,reject_s from "
						+ ff.getTableName() + " where instanceId in(" + process + ") ");
			else {
				sql.append("  select id,null as unit,to_date(createtime,'yyyy-mm-dd') as time,null as title,TO_CHAR(USERID) as userid,flowid,null,flowid as flow,INSTANCEID,reject_s from "
						+ ff.getTableName()
						+ " where  reject='true' and instanceId is null and userId ="
						+ user.getId()
						+ " or reject='true' and instanceId in(" + process + ")");
			}
			if (i != oflowform.size() - 1) {
				sql.append(" union all ");
			}
		}
		sql.append(" ) t where 1=1 ");
		if ((name != null) && (!"".equals(name))) {
			sql.append(" and t.flow like '%" + name.trim() + "%'");
		}
		if ((startDate != null) && (!"".equals(startDate.trim()))) {
			sql.append(" and t.time >= to_date('" + startDate + "','yyyy-mm-dd')");
		}
		if ((endDate != null) && (!"".equals(endDate.trim()))) {
			sql.append(" and t.time <= to_date('" + endDate + "','yyyy-mm-dd')");
		}
		sql.append(" order by t.id desc");
		List<Object[]> listob = this.fielTransactDao.findByPageSql(sql.toString(), currentPage);
		for (Object[] obj : listob) {
			FielAll fa = new FielAll();
			fa.setId(Integer.valueOf(((BigDecimal) obj[0]).intValue()));
			fa.setUnit((String) obj[1]);
			fa.setTime((Date) obj[2]);
			fa.setTitle((String) obj[3]);
			fa.setSender((String) obj[4]);
			fa.setFielTyoe((String) obj[5]);
			fa.setSort((String) obj[6]);
			fa.setFlow((String) obj[7]);
			fa.setEndTime(obj[8] == null ? "1" : ((BigDecimal) obj[8]).toString());
			fa.setReject((String) obj[9]);
			faList.add(fa);
		}
		return faList;
	}

	public String getUserAllOrgName(int orgId) {
		String sql = "select type,name from o_org start with id=" + orgId + "  connect by prior pid=id order by id desc";
		List<Object[]> list = this.fielTransactDao.findBySql(sql);
		String orgName = "";
		for (Object[] objs : list) {
			if (orgName.equals(""))
				orgName = objs[1].toString();
			else {
				orgName = objs[1].toString() + "." + orgName;
			}
			if (objs[0].equals("1")) {
				break;
			}
		}
		return orgName;
	}

	public OWorkflow getWorkFlow(int id) {
		OWorkflow flow = (OWorkflow) this.fielTransactDao.getObject(new OWorkflow(), id);
		if (flow != null) {
			if ((flow.getTypeId() == null) && (flow.getFormId().intValue() == 1))
				flow.setTypeName("发文");
			else if ((flow.getTypeId() == null) && (flow.getFormId().intValue() == 2))
				flow.setTypeName("收文");
			else {
				flow.setTypeName("工作流程");
			}
		}
		return flow;
	}

	public String listToString(List<OProcessinstance> list) {
		String id = null;
		if ((list != null) && (!list.isEmpty())) {
			for (int i = 0; i < list.size(); i++) {
				if (id == "")
					id = id + ((OProcessinstance) list.get(i)).getId();
				else {
					id = id + "," + ((OProcessinstance) list.get(i)).getId();
				}
			}
		}

		return id;
	}

	public FielTransactDao getFielTransactDao() {
		return this.fielTransactDao;
	}

	public void setFielTransactDao(FielTransactDao fielTransactDao) {
		this.fielTransactDao = fielTransactDao;
	}
}